<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>弹幕显示</title>
  <style>
    /* 弹幕容器样式 */
    .danmu-container {
      position: fixed;
      top: 50%;
      transform: translateY(-50%);
      white-space: nowrap;
      overflow: hidden;
      pointer-events: none; /* 防止干扰用户输入 */
    }

    /* 弹幕样式 */
    .danmu {
      position: absolute;
      font-size: 16px;
      color: #fa0404;
      animation: danmuAnimation 5s linear;
    }

    /* 弹幕飘过动画 */
    @keyframes danmuAnimation {
      from {
        transform: translateX(100vw);
      }
      to {
        transform: translateX(-100%);
      }
    }
  </style>
</head>
<body>

<!-- 输入框 -->
<input type="text" id="danmuInput" placeholder="输入弹幕内容">

<!-- 按钮触发弹幕显示 -->
<button onclick="sendDanmu()">发送弹幕</button>

<!-- 弹幕容器 -->
<div class="danmu-container" id="danmuContainer"></div>

<script>
  // 获取输入框和弹幕容器
  var danmuInput = document.getElementById('danmuInput');
  var danmuContainer = document.getElementById('danmuContainer');

  // 按钮触发弹幕显示
  function sendDanmu() {
    // 创建弹幕元素
    var danmuElement = document.createElement('div');
    danmuElement.className = 'danmu';
    danmuElement.innerText = danmuInput.value;

    // 将弹幕添加到容器中
    danmuContainer.appendChild(danmuElement);

    // 清空输入框内容
    danmuInput.value = '';

    // 弹幕飘过结束后移除弹幕元素
    danmuElement.addEventListener('animationend', function() {
      danmuElement.remove();
    });
  }
</script>

</body>
</html>
